package Servlet;

import Dao.Impl.DaoImpl;
import bean.Emp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录后台处理
 * Servlet implementation class LoginServlet
 */
@WebServlet(name = "loginServlet", value = "/loginServlet")
public class loginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName = req.getParameter("userName");
        String passWord = req.getParameter("passWord");
        String flag = req.getParameter("flag");

        DaoImpl dao = new DaoImpl();
        Emp emp = dao.login(userName);
        if (emp!=null){
            if (emp.getPass().equals(passWord)){
                if (flag!=null && !flag.equals("")){
                    Cookie c1 = new Cookie("userName",userName);
                    Cookie c2 = new Cookie("passWord",passWord);

                    c1.setMaxAge(60*60);
                    c2.setMaxAge(60*60);

                    resp.addCookie(c1);
                    resp.addCookie(c2);
                }
                req.getRequestDispatcher("showServlet").forward(req,resp);
            }else {
                req.setAttribute("msg","密码错误");
            }
        }else {
            req.setAttribute("msg","店铺不存在");
        }
        req.getRequestDispatcher("index.jsp").forward(req,resp);
    }
}
